package com.zhongke.cotrun.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhongke.cotrun.mapper.LevelOneCommentMapper;
import com.zhongke.cotrun.model.pojo.LevelOneComment;
import com.zhongke.cotrun.model.pojo.LevelTwoComment;
import com.zhongke.cotrun.model.pojo.Tags;
import com.zhongke.cotrun.service.LevelOneCommentService;
import com.zhongke.cotrun.service.LevelTwoCommentService;
import com.zhongke.cotrun.service.TagsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.List;

@Service
public class LevelOneCommentServiceImpl implements LevelOneCommentService {

    @Autowired
    LevelOneCommentMapper levelOneCommentMapper;//一级评论
    @Autowired
    LevelTwoCommentService levelTwoCommentService;//二级评论
    @Autowired
    TagsService tagsService;//点赞

    /**
     * 根据项目id查看一级评论
     *
     * @param projectId
     * @return
     */
    @Override
    public PageInfo selectLevelOneCommentByprojetId(Integer projectId,String type, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        LevelOneComment levelOneComment = new LevelOneComment();
        levelOneComment.setLinkProjectId(projectId);
        levelOneComment.setType(type);

        /**
         * 查出所有的一级评论
         */
        List<LevelOneComment> l = levelOneCommentMapper.selectLevelOneComment(levelOneComment);
        levelOneComment = null;
        if (l == null || l.size() == 0) {
            return null;
        }
        for (LevelOneComment lc : l) {

            //查询一级的点赞数
            Tags tags = new Tags();
            tags.setLinkCommentId(lc.getLevelOneCommentId());
            tags.setType(type);
            tags.setGarde(1);
            lc.setTags(tagsService.selectTagesByLevelOneCommentId(tags));
            tags = null;
            //查询所有二级的评论
            LevelTwoComment levelTwoComment = new LevelTwoComment();
            levelTwoComment.setLinkCommentId(lc.getLevelOneCommentId());
            levelTwoComment.setType(type);
            lc.setLevelTwoComments(levelTwoCommentService.selectLevelTwoComment(levelTwoComment));
            levelTwoComment = null;

        }

        return new PageInfo(l);
    }

    /**
     * 新建一级评论
     *
     * @param levelOneComment
     * @return
     */
    @Override
    @Transactional
    public LevelOneComment insertLevelOneComment(LevelOneComment levelOneComment) {
        if (levelOneCommentMapper.insertLevelOneComment(levelOneComment)) {
            Tags tags = new Tags();
            tags.setLinkCommentId(levelOneComment.getLevelOneCommentId());
            tags.setGarde(1);
            tags.setType(levelOneComment.getType());
         if(tagsService.insertTags(tags)) {
             levelOneComment.setTags(tags);
             return levelOneComment;
         }
        }
        return null;
    }

    /**
     * 根据id产看一级评论
     *
     * @param id
     * @return
     */
    @Override
    public LevelOneComment selectLevelOneCommentById(BigInteger id ,String type) {

        LevelOneComment levelOneComment = new LevelOneComment();
        levelOneComment.setLevelOneCommentId(id);
        levelOneComment.setType(type);
        /**
         * 查出所有的一级评论
         */
        List<LevelOneComment> l = levelOneCommentMapper.selectLevelOneComment(levelOneComment);
        levelOneComment = null;
        if (l == null || l.size() == 0) {
            return null;
        }
        for (LevelOneComment lc : l) {

            //查询一级的点赞数
            Tags tags = new Tags();
            tags.setLinkCommentId(id);
            tags.setType(type);
            tags.setGarde(1);
            lc.setTags(tagsService.selectTagesByLevelOneCommentId(tags));
            tags = null;
            //查询所有二级的评论
            LevelTwoComment levelTwoComment = new LevelTwoComment();
            levelTwoComment.setLinkCommentId(id);
            levelTwoComment.setType(type);
            lc.setLevelTwoComments(levelTwoCommentService.selectLevelTwoComment(levelTwoComment));
            levelTwoComment = null;

        }
        return l.get(0);
    }

    /**
     * 修改评论状态
     *
     * @param state             状态
     * @param levelOneCommentId 评论 的id
     * @param userId            用户的id
     * @return
     */
    @Override
    public Boolean updateStateLevelOneComment(Integer state, Integer levelOneCommentId, Integer userId) {
        Integer a = levelOneCommentMapper.seleOneCommentLinkManById(levelOneCommentId);
        if (a != userId) {
            return false;
        }

        return  levelOneCommentMapper.updateStateForLevelOneComment(state,levelOneCommentId);
    }
}
